home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / bbsutil / bsrc_250.zip / ASYNC.C next >
C/C++ Source or Header  |  1991-09-15  |  21KB  |  714 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-91, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*               This module was written by Peter Fitzsimmons               */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*                 BinkleyTerm OS/2 Async Comm I/O Routines                 */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.250.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. gg/*                                Internet f491.n343.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*--------------------------------------------------------------------------*/
  45.  
  46. #ifndef OS_2
  47. #pragma message("This Module For OS/2")
  48. #else
  49. #include <stdio.h>
  50. #include <stdarg.h>
  51. #include <ctype.h>
  52. #include <conio.h>
  53. #include <string.h>
  54. #include <stdlib.h>
  55. #define INCL_DOS
  56. #define INCL_DOSDEVICES
  57. #define INCL_DOSDEVIOCTL
  58. #define INCL_DOSSEMAPHORES
  59. #define INCL_NOPM
  60. #include <os2.h>
  61. #include "bink.h"
  62. #include "com.h"
  63. #include "async.h"
  64.  
  65. /*--------------------------------------------------------*
  66.  * This file contains the operating system specific       *
  67.  * serial communications routines for OS/2.               *
  68.  *                                                        *
  69.  * (C) Copyright Peter Fitzsimmons Sun  04-16-1989        *
  70.  *                                                        *
  71.  * Add-ins for BinkleyTerm on Sat  05-06-1989             *
  72.  *                                                        *
  73.  * Worked over once again on 06/06-91, trying to get the  *
  74.  * same effect as the Rev 6 FOSSIL in it                  *
  75.  *--------------------------------------------------------*/
  76.  
  77. /* This one has excellent send or receive, even with 8088 */
  78. /* but the xmit rate is a bit low in Janus w/o SetPrty.   */
  79.  
  80.  
  81. /* #define DEBUG */           /* Exposes a bit more about ASYNC comm */
  82. /* #define MultiWrt */        /* Not always reliable                 */
  83.  
  84.  
  85. extern void status_line (char *,...);
  86. extern void com_kick (void);
  87.  
  88. /* Private data */
  89. /* static HFILE hfComHandle = -1; */     /* handle from DosOpen() */
  90. HFILE hfComHandle = -1;      /* handle from DosOpen() */
  91.  
  92. /* transmitter stuff */
  93. #define TSIZE 8192
  94. static unsigned char tBuf[TSIZE];
  95. static unsigned char zTxBuf[TSIZE];
  96. static int zpos = 0;
  97. static int tBufsize = 0;
  98. static unsigned int TQSize = 0;
  99. /* static HSEM WriteSem = 0; */
  100. ULONG WriteSem = 0;
  101.  
  102. /* receiver stuff */
  103. #define RSIZE 8192
  104. static unsigned char rbuf[RSIZE];
  105. static USHORT rpos = 0;
  106. static int rbufsize = 0;
  107. static USHORT Rbytes = 0;
  108. static word RQBbytes = 0;
  109. static word RQBbyte2 = 0;
  110.  
  111. #ifdef DEBUG
  112.  
  113. #define debug_msg(m,c)  status_line("!" m, c)
  114. #define IOCtl(func, data, parm)  _ioctl(#func, func, (void far *) data, (void far *) parm)
  115.  
  116. static int _ioctl(char *funcname, int func, void far * data, void far * parm)
  117. {
  118.     int i;
  119.  
  120.     if (i = DosDevIOCtl((PVOID) data, (PVOID) parm, func, IOCTL_ASYNC, (HFILE) hfComHandle)) {
  121.         printf("ioctl(%s) err(0x%04x)\n", funcname, i);
  122.         status_line("!ioctl(%s) err(0x%04x)", funcname, i);
  123.     }
  124.     return (i);
  125. }
  126.  
  127. #else
  128. #define debug_msg(m,c)
  129. #define IOCtl(func, data, parm) DosDevIOCtl((PVOID) data, (PVOID) parm, func, IOCTL_ASYNC, (HFILE) hfComHandle)
  130. #endif
  131.  
  132. static int com_getDCB(struct _DCBINFO far * dcb);
  133. static int com_setDCB(struct _DCBINFO far * dcb);
  134.  
  135.  
  136. /* com_init() : Intialize communications port. Baud rate is preserved.
  137.  *            int port  : Hardware port (0 based) to init
  138.  *    -OR-  char *unc   : Full UNC \\networkId\modemId to init.
  139.  *
  140.  * if unc==NULL, port is used. if unc != NULL, unc is used
  141.  */
  142. int com_init(int port, char far *unc)
  143. {
  144.     char str[30];
  145.     char *s;
  146.     USHORT ActionTaken;         /* action: returned by OS/2 */
  147.     USHORT stat;
  148.     DCBINFO sDCB;
  149.     RXQUEUE q;
  150.  
  151.     cputs("\033[1;33mOS/2 FOSSIL emulator for BinkleyTerm, Peter Fitzsimmons (1:250/628)\033[0m\r\n");
  152.     sprintf(str, "COM%d", port + 1);
  153.     if (!unc)
  154.        unc = str;
  155. #ifdef DEBUG
  156.     stat = DosOpen((PSZ) unc,(PHFILE) &hfComHandle,(PUSHORT) &ActionTaken, 0L, 0, 0x0001, 0x4012, 0L);
  157. #else
  158.     stat = DosOpen((PSZ) unc,(PHFILE) &hfComHandle,(PUSHORT) &ActionTaken, 0L, 0, 0x0001, 0x6012, 0L);
  159. #endif
  160.     if (stat) {
  161.        hfComHandle = -1;
  162.        printf("com_init() : DosOpen() error 0x%04x on '%s'\n", stat, unc);
  163.        return (FALSE);
  164.     }
  165.     (void) DosSemClear((HSEM) &WriteSem);
  166.     if (!IOCtl(ASYNC_GETINQUECOUNT, (PVOID) &q, (PVOID) 0L)) {
  167.        s = getenv("RBUF");
  168.        if (s)
  169.            rbufsize = min( RSIZE, atoi(s));
  170.        else
  171.            rbufsize = RSIZE;
  172.        RQBbytes = min( RSIZE, rbufsize);
  173.        RQBbyte2 = RQBbytes/2;
  174. #ifdef DEBUG
  175.        printf("com_init() : ASYNC Input Buffer size is %d'\n", q.cb);
  176. #endif
  177.     }
  178.     if (!IOCtl(ASYNC_GETOUTQUECOUNT, (PVOID) &q, (PVOID) 0L)) {
  179.        s = getenv("TBUF");
  180.        if (s)
  181.            tBufsize = min( TSIZE, atoi(s));
  182.        else
  183.            tBufsize = TSIZE;
  184. /*     TQSize = min(TSIZE, q.cb/2);    */    /* WRA */
  185.        TQSize = min(TSIZE, q.cb-16);
  186. #ifdef DEBUG
  187.        printf("com_init() : ASYNC Output Buffer size is %d'\n", q.cb);
  188. #endif
  189.     }
  190.     com_XON_disable();
  191.  
  192.     com_getDCB((struct _DCBINFO far *) &sDCB);